package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.ISystemDictionaryItemService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.AjaxResult;
import cn.wolfcode.p2p.base.util.RequireLogin;
import cn.wolfcode.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BasicInfoController {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequireLogin
    @RequestMapping("/basicInfo")
    public String basicInfoPage(Model model) {
        model.addAttribute("userInfo", userInfoService.getCurrent());

        model.addAttribute("educationBackgrounds", systemDictionaryItemService.queryListByParentSn("educationBackground"));
        model.addAttribute("incomeGrades", systemDictionaryItemService.queryListByParentSn("incomeGrade"));
        model.addAttribute("marriages", systemDictionaryItemService.queryListByParentSn("marriage"));
        model.addAttribute("kidCounts", systemDictionaryItemService.queryListByParentSn("kidCount"));
        model.addAttribute("houseConditions", systemDictionaryItemService.queryListByParentSn("houseCondition"));
        return "basicInfo";
    }

    @RequestMapping("/basicInfo_save")
    @ResponseBody
    public AjaxResult basicInfoSave(UserInfo userInfo) {
        AjaxResult result = null;
        try {
            userInfoService.basicInfoSave(userInfo);
            result = new AjaxResult("保存成功");
        } catch (DisplayableException e) {
            e.printStackTrace();
            //如果底层是抛出这个异常的话 则把 异常信息显示给用户看
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            //把异常信息写入到日志中
            result = new AjaxResult(false, "系统异常,程序员哥哥正在努力抢救中...");
        }
        return result;
    }
}
